import React from 'react';
import { MenuTrigger, getContextMenu } from '../components/ContextMenu';

export default function ContextMenuPlugin(options = {}) {
  const { getMenus = () => [], menuId } = options;
  const ContextMenu = getContextMenu(menuId);
  return {
    renderEditor: (props, editor, next) => {
      return (
        <>
          {next(props)}
          <ContextMenu />
        </>
      );
    },
    renderDecorator: (props, editor, next) => {
      const menus = getMenus(props);
      if (Array.isArray(menus) && menus.length > 0) {
        return (
          <MenuTrigger {...props} menuId={menuId} editor={editor} menus={menus}>
            {next(props)}
          </MenuTrigger>
        );
      }
      return next(props);
    }
  };
}
